<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>购物车</title>

    <%--    样式--%>
    <%@include file="/pages/common/plug_.jsp" %>
    <%--    样式--%>
    <script type="text/javascript">
        $(function () {
            $("a.deleteItem").click(function () {
                return confirm("你确定删除"+ $(this).parent().parent().find("td:first").text() +"吗?")
            });
            $("#clear_id").click(function () {
                return confirm("你确定清空购物车吗?");
            });
            $(".updateCount").change(function () {
                var name = $(this).parent().parent().find("td:first").text();
                var count = this.value;
                var id = $(this).attr("cartItemId");
                if (confirm("你确定要将【"+ name +"】的数量改为"+ count +"吗?")) {
                    location.href = "${pageScope.Path}/cartServlet?action=updateCount&count="+count+"&id=" + id;
                }else {
                    this.value = this.defaultValue;
                }
            })
        })
    </script>


</head>
<body>
<!-- header -->
<%@include file="/pages/common/success_head.jsp" %>
<div id="header">
    <span class="wel_word">购物车</span>
    <%@include file="/pages/common/navigation_bar.jsp" %>
</div>
<!-- header -->

<div id="main">

    <table>
        <tr>
            <td>商品名称</td>
            <td>数量</td>
            <td>单价</td>
            <td>金额</td>
            <td>操作</td>
        </tr>
        <C:if test="${empty sessionScope.cart}">
            <tr>
                <td>
                    <a href="${pageScope.Path}/index.jsp">购物车为空,去购物</a></td>
            </tr>
        </C:if>
        <C:forEach items="${sessionScope.cart.items}" var="cartItem">
            <tr>
                <td>${cartItem.value.name}</td>
                <td><input type="text" class="updateCount"
                           cartItemId="${cartItem.value.id}"
                           style="width: 80px" value="${cartItem.value.count}"></td>
                <td>${cartItem.value.price}</td>
                <td>${cartItem.value.totalPrice}</td>
                <td><a class="deleteItem" href="${pageScope.Path}/cartServlet?action=deleteItem&id=${cartItem.value.id}">删除</a></td>
            </tr>
        </C:forEach>

    </table>
    <C:if test="${not empty sessionScope.cart.items}">
        <div class="cart_info">
        <span class="cart_span">
            购物车中共有<span class="b_count">${sessionScope.cart.totalCount}</span>件商品</span>
            <span class="cart_span">
            总金额<span class="b_price">${sessionScope.cart.totalPrice}</span>元</span>
            <span class="cart_span">
                <a id="clear_id" href="${pageScope.Path}/cartServlet?action=clearItem">清空购物车</a></span>
            <span class="cart_span">
                <a href="${pageScope.Path}/orderServlet?action=createOrder">去结账</a></span>
        </div>
    </C:if>
</div>


<!--footer-->
<%@include file="/pages/common/common_footer.jsp" %>
<!--footer-->


</body>
</html>